package com.maxus.mini.ecard.open.api.controller.ecard;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maxus.mini.common.PageImplE;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.vo.ResBody;
import com.maxus.mini.ecard.data.api.ecard.ECardService;
import com.maxus.mini.ecard.data.api.user.UserService;
import com.maxus.mini.ecard.entity.ecard.ECard;
import com.maxus.mini.ecard.entity.user.User;
import com.maxus.mini.ecard.entity.vo.ecard.ECardVo;
import com.maxus.mini.ecard.open.api.controller.BaseController;
import com.maxus.mini.ecard.open.api.vo.req.P100001Vo;
import com.maxus.mini.ecard.open.api.vo.req.P800002Vo;
import com.maxus.mini.ecard.open.api.vo.req.P800003Vo;
import com.maxus.mini.ecard.open.api.vo.req.P800004Vo;
import com.maxus.mini.ecard.open.api.vo.res.PR100001Vo;
import com.maxus.mini.ecard.open.api.vo.res.PR800002Vo;
import com.maxus.mini.ecard.open.api.vo.res.PR800003Vo;
import com.maxus.mini.ecard.open.api.vo.res.PR800004Vo;

/**
 * 贺卡controller
 * 
 * @author wujinsong
 *
 */
@RestController()
@RequestMapping(value = "api/v1.0/ecard")
public class ECardController extends BaseController {
	@Reference(version = "1.0.0")
	private ECardService eCardService;
	@Reference(version = "1.0.0")
	private UserService<User, User> userService;
	@Reference(version = "1.0.0")

	/**
	 * 贺卡模板列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody list(@RequestBody P800002Vo req) {
		PR800002Vo retVo = new PR800002Vo();
		List<ECard> list = null;

		ECardVo vo = new ECardVo();
		try {
			BeanUtilsExtended.copyProperties(vo, req);
			vo.setIsTemplate(Boolean.TRUE);
			PageImplE<ECard> pageImplE = eCardService.page(vo);
			list = pageImplE.getContent();
			retVo.setTotalElements(pageImplE.getTotalElements());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		retVo.setItemVos(list);

		return retVo;
	}

	/**
	 * 获取我的贺卡列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "ilist", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody ilist(@RequestBody P800003Vo req) {
		PR800003Vo retVo = new PR800003Vo();
		List<ECard> list = null;

		ECardVo vo = new ECardVo();
		BeanUtilsExtended.copyProperties(vo, req);
		vo.setIsTemplate(Boolean.FALSE);
		vo.setUserId(req.getUserId());
		try {
			PageImplE<ECard> pageImplE = eCardService.page(vo);
			if(pageImplE.getNumberOfElements() % 2 != 0) {
				pageImplE.getContent().add(new ECard());
			}
			list = pageImplE.getContent();
			retVo.setTotalElements(pageImplE.getTotalElements());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		retVo.setItemVos(list);

		return retVo;
	}

	/**
	 * 新增贺卡
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody()
	public ResBody add(@RequestBody P100001Vo req) {
		PR100001Vo retVo = new PR100001Vo();
		User user = userService.find(req.getUserId());
		ECard eCard = new ECard();
		BeanUtilsExtended.copyProperties(eCard, req);
		eCard.setTemplate(false);
		eCard.setUser(user);
		eCard.setPicUrl(req.getPicUrl());
		
		ECard parent = eCardService.find(req.getTemplateId());
		eCard.setParent(parent);
		eCardService.save(eCard);

		return retVo;
	}
	
	/**
	 * 获取电子贺卡详情
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "detail")
	@ResponseBody()
	public ResBody detail(@RequestBody P800004Vo req) {
		PR800004Vo retVo = new PR800004Vo();
		
		ECard eCard = eCardService.find(req.getId());
		if(eCard.isTemplate()) {
			eCard.setUsed(eCard.getUsed()+1);
			eCardService.save(eCard);
		}
		retVo.setDetail(eCard);

		return retVo;
	}

}
